import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { useStoreUser, useStoreApp } from '@/store';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import NProgress from 'nprogress'; // progress bar
import 'nprogress/css/nprogress.css'; // 进度条样式
import mainRoute from './main'
import { postMpAuthWxLogin } from '@/api';
import { GetParam } from "@/utils/tools";

NProgress.configure({ showSpinner: false }); // NProgress Configuration

// 基础路由
const constRoute: Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'login',
        meta: {
            title: '登录',
        },
        component: () => import(/* webpackChunkName: "about" */ '@/views/login/index.vue'),
    },
    {
        path: '/reset/pwd',
        name: 'reset-pwd',
        meta: {
            title: '忘记密码',
        },
        component: () => import(/* webpackChunkName: "about" */ '@/views/login/resetPwd.vue'),
    },
    ...mainRoute,
];
// 白名单路
const whiteList = ['login', 'reset-pwd']

console.log('whiteList', whiteList);

// 动态路由前端路由表(需要与后台返回菜单匹配过滤)
// export const asyncRoute: Array<RouteRecordRaw> = [...propertyRoute, ...tenantRoute];
const routes: Array<RouteRecordRaw> = [...constRoute];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to: any, from: any, next: any) => {


    const storeUser = useStoreUser();
    const storeApp = useStoreApp();
    NProgress.start();
    if (to.name && whiteList.includes(to.name)) {
        next();
    }
    console.log('storeUser.dlsToken', storeUser.dlsToken);
    console.log('最新的包11');

    // 开发环境下模拟token
    // storeUser.dlsToken = '3425f734-d21a-4d13-80c5-1bbbaa5b4dc8'
    // 根据是否存在token判断登录
    if (storeUser.dlsToken) {
        next()
        let currentActive = '';
        if (to.name) {
            switch (to.name) {
                case 'business':
                    currentActive = '0';
                    break;
                case 'wallet':
                    currentActive = '1';
                    break;
                // case 'customer':
                //     currentActive = '2';
                //     break;
                // case 'mine':
                //     currentActive = '3';
                //     break;
            }
        }
        console.log('currentActive', currentActive);

        storeApp.homeActive = currentActive
    } else {
        // 线上环境
        const openId = GetParam(window.location.href, "openId");
        if (!openId) {
            postMpAuthWxLogin().then((res: { data: { url: string; }; }) => {
                console.log('res.data.url', res.data.url);

                // debugger
                window.location.href = res.data.url
            })
            return
        } else {
            next();
        }
        // 开发环境下
        // next();
        // next({
        //     path: '/login',
        // });
    }

    document.title = to.meta.title;
});

router.afterEach(() => {
    NProgress.done();
    // document.body.scrollTo(0, 0)
});

export default router;
